﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.MEP
{
	[Transaction(TransactionMode.Manual)]
	public class HyDQClearRangeCheck : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			Document document = commandData.Application.ActiveUIDocument.Document;
			Result result = 0;
			Transaction transaction = new Transaction(document, "清除保护范围");
			transaction.Start();
			try
			{
				ElementCategoryFilter item = new ElementCategoryFilter(BuiltInCategory.OST_FireAlarmDevices);
				List<ElementFilter> elementFilterList = new List<ElementFilter>
				{
					item
				};
				new ClearProtectRange(document, elementFilterList, this.rangeCheckApplicatScenarios);
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
                    if ((int)status == 1)
					{
						transaction.Commit();
					}
                    else if ((int)status == 5 || (int)status == 6)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}

		private HyRangeCheckMajor rangeCheckApplicatScenarios = HyRangeCheckMajor.HyDQ;
	}
}
